#!/bin/sh
CTIME=`date "+%Y-%m-%d %H:%M:%S"`

echo "\n"

echo "Launch time: ${CTIME} \n"

help() {   
    echo "Usage: ./launch.sh [--options=arg] [...] \n"
    echo "Options: "
    echo "   --protocol      : 请求协议，http 或 https\n"
    echo "   --domain        : 请求域名 或 IP\n"
    echo "   --port          : 请求端口，如果协议是https需要添加端口\n"
    echo "   --threadNum     : 线程数量，默认启动1个线程\n"
    echo "   --rampupTime    : 加载时间，默认1秒内所有线程全部启动\n"
    echo "   --loopNum       : 循环次数，默认循环1次，参数输入'-1'永远循环\n"
    # echo "   --aKey          : 应用秘钥，最多传入5个appKey，并用','分隔\n"
    echo "   --aKey          : App Key\n"
    echo "   --sKey          : Secret Key\n"
    echo "   --doctor        : 检查运行环境并退出\n"
    echo "   --help          : 查看帮助文档并退出\n"
    exit 0
}

doctor() {
    echo "Diagnostic starting...\n"
    # JAVA_VERS=`java -version 2>&1 | sed '1!d' | sed -e 's/"//g' | awk '{print $3}'`;
    # if [[ ${JAVA_VERS} != 11.* ]]; then
    #     echo "  Error: Please check java running environment!\n"
    #     exit 1
    # else
    #     echo "  Java version is ${JAVA_VERS}.\n"
    #     JMET_VERS=`jmeter -v 2>&1 | sed '5!d' | sed -e 's/"//g' | awk '{print $11}'`;
    #     if [[ ${JMET_VERS} != 5.4.* ]]; then
    #         echo "  Error: Please check jmeter running environment!\n"
    #         exit 1
    #     fi
        # echo "  JMeter version is ${JMET_VERS}.\n"
    # fi

    JAVA_VERS=`java -version`
    echo "${JAVA_VERS}\n"

    JMET_VERS=`jmeter -v`
    echo "${JMET_VERS}\n"

    echo "Diagnostic completed, running environment is ok."
    echo "Everything looks good, bye!\n"
    exit 0
}


if [[ $# == 0 || "$1" == "--doctor" ]]; then
    doctor
    exit 0
fi


if [[ $# == 0 || "$1" == "--help" ]]; then
    help
    exit 0
fi


for i in "$@"; 
do
    case $i in
        --protocol=*)
            PROTOCOL="${i#*=}"
            ;;
        --domain=*)
            DOMAIN="${i#*=}"
            ;;
        --port=*)
            PORT="${i#*=}"
            ;;
        --threadNum=*)
            THREAD_NUM="${i#*=}"
            ;;
        --rampupTime=*)
            RAMPUP_TIME="${i#*=}"
            ;;
        --loopNum=*)
            LOOP_NUM="${i#*=}"
            ;;
		--sKey=*)
			SECKEY="${i#*=}"
            ;;
        --aKey=*)
            APPKEY="${i#*=}"
            ;;
        *)
            echo "  Error: Options wrong, please refer to the help documentation using '--help'!\n"
            help
            exit 1
            ;;
    esac
done


if [[ ! $@ =~ "--protocol" ]]; then
    echo "  Error: Required option '--protocol' is missing, plea! refer to the help documentation using '--help'!\n"
    exit 1
else
    if [[ ${PROTOCOL} == "" ]]; then
        echo "  Error: '--protocol' params is empty!"
        exit 1
    else
        if [[ ${PROTOCOL} != "http" && ${PROTOCOL} != "https" ]]; then
            echo "  Error: '--protocol' only supports 'http' or 'https'!\n"
            exit 1
        fi
    fi
fi

if [[ ! $@ =~ "--domain" ]]; then
    echo "  Error: Required option '--domain' is missing, please refer to the help documentation using '--help'!\n"
    exit 1
else
    if [[ ${DOMAIN} == "" ]]; then
        echo "  Error: '--domain' params is empty!\n"
        exit 1
    fi
fi

if [[ ! $@ =~ "--port" ]]; then
    echo "  Error: Required option '--port' is missing, please refer to the help documentation using '--help'!\n"
    exit 1
else
    if [[ ${PROTOCOL} == "https" && ${PORT} == "" ]]; then
        echo "  Error: '--port' params is empty!\n"
        exit 1
    fi
fi

if [[ ! $@ =~ "--aKey" ]]; then
    echo "  Error: Required option '--aKey' is missing, please refer to the help documentation using '--help'!\n"
    exit 1
else
    if [[ ${DOMAIN} == "" ]]; then
        echo "  Error: '--aKey' params is empty!\n"
        exit 1
    fi
fi

if [[ ! $@ =~ "--sKey" ]]; then
    echo "  Error: Required option '--sKey' is missing, please refer to the help documentation using '--help'!\n"
    exit 1
else
    if [[ ${DOMAIN} == "" ]]; then
        echo "  Error: '--sKey' params is empty!\n"
        exit 1
    fi
fi

# AKEY_LIST=(${APPKEYS//,/ });
# if [[ ! $@ =~ "--aKey" ]]; then
#     echo "  Error: Required option '--akey' is missing, please refer to the help documentation using '--help'!\n"
#     exit 1 
# else
#     if [[ ${#AKEY_LIST[*]} == 0 ]]; then
#         echo "  Error: '--aKey' params is empty!\n"
#         exit 1
#     elif [[ ${#AKEY_LIST[*]} > 5 ]]; then
#         echo "  Error: '--aKey' params exceeds the maximum limit!\n"
#         exit 1
#     fi
# fi

# OPTNAME="-JaKey"; 
# APKOPTS=""; 
# for ((i=1; i<=${#AKEY_LIST[*]}; i++)); 
# do
#     APKOPTS="${APKOPTS} ${OPTNAME}_${i}=${AKEY_LIST[${i}-1]}"
# done

RESDIR=result

if [ -d "${RESDIR}" ]; then
    rm -rf ${RESDIR}
fi

mkdir ${RESDIR}

JTL_FILE=${RESDIR}/result.jtl
HTML_DIR=${RESDIR}/report

# OPTIONS="-Jprotocol=${PROTOCOL} -Jdomain=${DOMAIN} -Jport=${PORT} -JthreadNum=${THREAD_NUM} -JrampupTime=${RAMPUP_TIME} -JloopNum=${LOOP_NUM} -Jakey=${APPKEY} -Jskey=${SECKEY}"
OPTIONS="-Jprotocol=${PROTOCOL} -Jdomain=${DOMAIN} -Jport=${PORT} -JaKey=${APPKEY} -JsKey=${SECKEY}"

LAUNCMD="jmeter -n -t ATS-Collect.jmx -l ${JTL_FILE} -e -o ${HTML_DIR} ${OPTIONS}"
echo "Running：${LAUNCMD} \n"
${LAUNCMD}

# OUTPUT=`${LAUNCH_SH}` 

# echo "${OUTPUT}"

# if [[ ! "${OUTPUT}" =~ "Warning: Nashorn engine" ]]; then
#     echo -e "  \nError: Running Exception, please use '--doctor' to check the runtime environment！\n"
#     exit 1
# fi


